﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.IO;
using Ks.Fs.Entity;
using Ks.Fs.Core.Services;
using Ks.Fs.Core.Common;

namespace Ks.FileStreaming
{
    
    public class StreamingService : IStreamingService
    {
        public RemoteFileInfo DownloadFile(DownloadRequest requestFile)
        {
            throw new NotImplementedException();
        }

        public UploadedFileInfo UploadFile(RemoteFileInfo request)
        {            
            Ks.Fs.Entity.File f = new Fs.Entity.File();
            if (string.IsNullOrEmpty(request.FileName))
            {
                f.Filename = Guid.NewGuid().ToString();
            }
            else
            {
                f.Filename = request.FileName;
            }            
            f.Owner = request.Owner;
            f.LastModified = DateTime.Now;
            f.FolderId = request.FolderId;
            f.Size = request.Length;           
            string uploadedPath = string.Empty;
            int fileId = FileService.WriteFile(request.FileByteStream,f,out uploadedPath);
            UploadedFileInfo uploadedResult = new UploadedFileInfo();
            uploadedResult.UploadedFileId = fileId;
            uploadedResult.Path = uploadedPath;
            return uploadedResult;
        }

        public bool Move(int fileId, int toFolderId)
        {
            return FileService.Move(fileId,toFolderId);
        }

        public bool Copy(int fileId, int toFolderId)
        {
            return FileService.Copy(fileId,toFolderId);
        }

        public bool DeleteFile(int fileId)
        {           
            return FileService.DeleteFile(fileId);
        }
        
        public List<Ks.Fs.Entity.File> GetFiles(int folderId,int sortBy,bool isSortAscending,int pageSize, int pageIndex)
        {
            return FileService.GetFiles(folderId,sortBy,isSortAscending,pageSize,pageIndex);
        }

        public int CreateFolder(Folder objFolder)
        {
            return FolderService.CreateFolder(objFolder);
        }

        public bool ChangeName(int folderId, string newFolderName)
        {
            return FolderService.ChangeName(folderId,newFolderName);
        }

        public bool DeleteFolder(int folderId)
        {
            return FolderService.DeleteFolder(folderId);
        }

        public List<Folder> GetFolders(int parentId, string owner,int sortBy,bool isSortAscending,int pageSize,int pageIndex)
        {
            return FolderService.GetFolders(parentId,owner,sortBy,isSortAscending,pageSize,pageIndex);
        }

        public Folder GetFolder(int folderId)
        {
            return FolderService.GetFolder(folderId);
        }

        public int CreateUser(User obj)
        {
            return UserService.CreateUser(obj);
        }

        public List<User> GetUsers()
        {
            return UserService.GetUsers();
        }
        
        public List<System.IO.DirectoryInfo> GetPhysicalFolders(int parentId)
        {
            Folder objParent = FolderService.GetFolder(parentId);
            if (objParent != null)
            {
                System.IO.DirectoryInfo dir = new DirectoryInfo(Path.Combine(Config.RootPath,objParent.Path));
                if (dir.Exists)
                {
                    return dir.GetDirectories().ToList();
                }
            }
            return new List<DirectoryInfo>();
        }
    }
}
